﻿using Entity.Models;
using HuaRunInterface.Comm;
using HuaRunInterface.Models;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HuaRunInterface.Controllers
{
    [Route("api/HuaRun/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        // POST api/<LoginController>
        [HttpPost]
        public object Post([FromBody] LoginModel value)
        {
            return Login(value);
        }

        private object Login(LoginModel dto)
        {
            ReturnData rd = new ReturnData();

            try
            {
                string conn = Global.Configuration["ConnectionStrings:DDb"];
                using (Entity.DBContext context = Entity.BaseContext.CreateContext(conn))
                {
                    LoginUserInfo user = context.loginUserInfos.Where(p => p.UserName == dto.UserName && p.UserPwd == dto.UserPwd).FirstOrDefault();
                    if (user != null)
                    {
                        rd.State = "success";
                    }
                    else
                    {
                        rd.State = "failed";
                        rd.Message = "登录名或者密码不正确，请稍后重试！";
                    }
                }
            }
            catch (Exception ex)
            {
                rd.State = "failed";
                rd.Message = ex.Message;
                rd.Data = ex.StackTrace;
            }
            return rd;
        }
    }

    public class LoginModel
    {
        public string? UserName { get; set; }//nvarchar(200),
        public string? UserPwd { get; set; }//nvarchar(100),
    }
}
